#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 40, INF = 0x3f3f3f3f;

int n, w[N];
int f[N][N];
int g[N][N];

void dfs(int l, int r)
{
    if (l > r) return;
    cout << g[l][r] << ' ';
    dfs(l, g[l][r] - 1);
    dfs(g[l][r] + 1, r);
}

void solve()
{
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> w[i];

    for (int i = 0;i <= n + 1;i++)
        for (int j = 0;j <= n + 1;j++)
            f[i][j] = 1;

    for (int i = 1;i <= n;i++) { f[i][i] = w[i]; g[i][i] = i; }

    for (int len = 2;len <= n;len++)
        for (int l = 1;l + len - 1 <= n;l++) {
            int r = l + len - 1;
            for (int k = l;k <= r;k++) if (f[l][r] < f[l][k - 1] * f[k + 1][r] + w[k]) {
                f[l][r] = f[l][k - 1] * f[k + 1][r] + w[k];
                g[l][r] = k;
            }
        }

    cout << f[1][n] << '\n';

    dfs(1, n);
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}